<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ page isELIgnored="false"%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Detail</title>
<script language="JavaScript">
	
function doDelete() {
	var checkboxName = "chk_meal";
	var deleteAction = "/deleteMeal.do";
	var formObj = document.form_mealList;
	if (!hasOneChecked(checkboxName)) {
		alert("请选择你要删除的对象!");
		return;
	}
	if (confirm("你确认要删除?")) {
		formObj.action = deleteAction;
		formObj.method = 'POST';	
		var m = document.createElement("input");
		m.setAttribute("type", "hidden");
		m.setAttribute("name", "userId");
		m.setAttribute("value", "admin");
		formObj.appendChild(m);

		var elms = document.getElementsByName("chk_meal");
		var meals = "";
		for(var i = 0; i < elms.length; i++) {
			if (elms[i].checked == true){
				meals += elms[i].value + ",";
			}
		}

		meals = meals.substr(0,meals.length-1);
		var m = document.createElement("input");
		m.setAttribute("type", "hidden");
		m.setAttribute("name", "mealIds");
		m.setAttribute("value", meals);

		formObj.appendChild(m);		

		formObj.submit();
	}
}

function hasOneChecked(name){
    var items = document.getElementsByName(name);
    if (items.length > 0) {
        for (var i = 0; i < items.length; i++){
            if (items[i].checked == true){
                return true;
            }
        }
    } else {
        if (items.checked == true) {
            return true;
        }
    }
    return false;
}

function setAllCheckboxState(name,state) {
	var elms = document.getElementsByName(name);
	for(var i = 0; i < elms.length; i++) {
		elms[i].checked = state;
	}
}
</script>
</head>

<body>

<h1>Order Details</h1>

<p><form method="post" name="form_mealList">

<table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5">
Following is the list of booked meal.



<br>
<br>
<tr>
<td><input type="checkbox" name="chk_meal_all" onclick="setAllCheckboxState('chk_meal',this.checked)"/>	
</td>
<td alignment="right" width="20%">MealId </td>
<td alignment="right" width="20%">MealName</td>
<td width="15%">	
</tr>

<c:forEach items="${meals}" var ="mealsList">
<tr>
<td><input type="checkbox" name="chk_meal" value='<c:out value ="${mealsList.mealId}"/>'/>	
</td>
<td alignment="right" width="20%"><c:out value ="${mealsList.mealId}"/></td>
<td alignment="right" width="20%"><c:out value ="${mealsList.mealName}"/></td>
<td width="15%">	
</tr>
</c:forEach>

</table>

<br>
<div class="handleControl">
<!--	<input type="submit" class="stdButton" style="width:80px" value="Search" onclick="getReferenceForm()"/>-->
	<input type="button" style="width:80px" value="Add" onclick="window.location = '/jsp/newMeal.jsp'"/>
	<input type="button" style="width:80px" value="Delete" onclick="doDelete()"/>
<div>
<br/>
<a href="/indexJSP.do">logout</a>
</body>
</html>
